#!python
# 
# Ed Brannin, 31 Aug 2007
# edbrannin@gmail.com
# 
# AG Easy-setup Bootstrapper
# This script assumes you're running it on Windows.
# Though it should be poaaible to build the thing on Linux as well,
#   such as with Debian's "nsis" package.
# There are a few windows-specific lines of code here, 
#   namely those that look for makensisw.exe.

import urllib
from urllib import urlretrieve
from os import path, makedirs, access, W_OK, X_OK, R_OK, system
import re
# urlretrieve( url[, filename[, reporthook[, data]]])

def download(url, target_dir="downloads"):
    filename = uri2filename(url)
    if not access(target_dir, W_OK | X_OK):
        makedirs(target_dir)
    if not access(path.join(target_dir, filename), R_OK):
        print "Downloading %s..." % filename
        urlretrieve(url, path.join(target_dir, filename))
        print "Done."
    else:
        print "We already have %s!" % filename

def uri2filename(uri):
    return re.sub(r"\?.*", "", path.basename(uri))


FILES_TO_DOWNLOAD = """
http://python.org/ftp/python/2.4.4/python-2.4.4.msi
http://www.mcs.anl.gov/fl/research/accessgrid/software/required/windows/pywin32-210.win32-py2.4.exe
http://www.mcs.anl.gov/fl/research/accessgrid/software/required/windows/wxPython2.6-win32-unicode-2.6.3.3-py24.exe

http://www-unix.mcs.anl.gov/fl/research/accessgrid/software/releases/3.1/windows/AGTk-3.1-final-Py-2.4.exe

http://www.accessgrid.org/files/VPCScreenProducerService.zip
http://iri.informatics.indiana.edu/~dcpiper/agvcr/windows/AGVCR_2.2.1_Installer.exe

http://lbrn.lsu.edu/portal/TigerBoardAG/TigerboardAG31_10Beta.agpkg3
http://lbrn.lsu.edu/portal/TigerBoardAG/TigerboardAG301Beta_Installation_Guide.pdf
http://lbrn.lsu.edu/portal/TigerBoardAG/TigerboardAG301Beta_Users_Manual.pdf

http://netmedia.gist.ac.kr/agdv/Download/AG3/ExtendedVideoProducerService.zip
http://netmedia.gist.ac.kr/agdv/Download/AG3/ExtendedVideoConsumerService.zip

""".split()

NSIS_URI = "http://prdownloads.sourceforge.net/nsis/nsis-2.30-setup.exe?download"

def main(file_list):
    # This is somewhat less necessary now.
    print "WARNING:"
    print "If you interrupt this and leave a file half-downloded,"
    print "IT WILL NOT NOTICE."
    print
    print "So if you stop this mid-download, make sure you delete the"
    print "partial file."
    
    target_dir = "installers"
    for uri in file_list:
        download(uri, target_dir)
        # print "Done."

    print "Now, I'm going to check for NSIS."
    makensisw = path.join("\\Program Files", "NSIS", "makensisw.exe")
    if not access( makensisw, X_OK  ):
        print "Downloading NSIS..."
        download(NSIS_URI, target_dir)
        nsis_filename = path.join( target_dir, uri2filename(NSIS_URI) )
        print "Installing NSIS..."
        system( nsis_filename )
    
    print "Now, I'm going to build the installer with makensisw.exe.  Relax and enjoy the show."

    system( """ "%s" %s """ % (makensisw, "ag_setup_full.nsi" ))
    print "Done!"

if __name__ == '__main__':
    main(FILES_TO_DOWNLOAD)
